<?php
class classProductService{
    public function getProductInfo($iProductID){

        if(!is_numeric($iProductID) or $iProductID<1) return '';

        $_ProductPrice = new classPriceExport();
        $_ProductPrice->getDBPriceVat($iProductID,&$fPrice,&$fTax);
        
        $sql_result	= mysql_query('select * from products where ID='.$iProductID);
        $sql_object	= mysql_fetch_object($sql_result);
        $result['ID']               = $sql_object->ID;
        $result['SC_ID']            = $sql_object->SC_ID;
        $result['Product_Code']     = $sql_object->Product_Code;
        $result['Product_Name']     = $sql_object->Product_Name;
        $result['Product_Price']    = $fPrice;
        $result['Product_DPH']      = $sql_object->Product_DPH;
        $result['Product_UnitType'] = $sql_object->Product_UnitType;
        $result['Product_Desc']     = $sql_object->Product_Descr;
        
        return $result;
    }

    public function getTopSubCategoryInfo($iSC_ID){

        if(!is_numeric($iSC_ID) or $iSC_ID<1) return '';

        $sql_result=mysql_query("
            SELECT TC_Caption, SC_Caption
            FROM top_category, sub_category
            WHERE(
                sub_category.ID = $iSC_ID AND
                sub_category.TC_ID = top_category.ID
            )
        ");
        $sql_object = mysql_fetch_object($sql_result);
        $result['TC_Caption']   = $sql_object->TC_Caption;
        $result['SC_Caption']   = $sql_object->SC_Caption;

        return $result;
    }

    public function getProductAndCategoryInfo($iP_ID){

        if(!is_numeric($iP_ID) or $iP_ID<1) return '';

        $sql_result=mysql_query("
            SELECT TC_Caption, SC_Caption, Product_Name
            FROM top_category, sub_category, products
            WHERE(
                products.ID = $iP_ID AND
                sub_category.ID = products.SC_ID AND
                sub_category.TC_ID = top_category.ID
            )
        ");
        $sql_object = mysql_fetch_object($sql_result);

        $result['Product_Name'] = $sql_object->Product_Name;
        $result['TC_Caption']   = $sql_object->TC_Caption;
        $result['SC_Caption']   = $sql_object->SC_Caption;

        return $result;
    }

    public function getCategoryAndProductName(){

        function getTopSubCategoryInfo($iSC_ID){
            if(!is_numeric($iSC_ID) or $iSC_ID<1) return '';
            $sql_result=mysql_query("
                SELECT TC_Caption, SC_Caption, TC_Have_SC
                FROM top_category, sub_category
                WHERE(
                    sub_category.ID = $iSC_ID AND
                    sub_category.TC_ID = top_category.ID
                )
            ");
            $sql_object = mysql_fetch_object($sql_result);
            if($sql_object->TC_Have_SC!=1)
               return ' - '.$sql_object->TC_Caption;
            else
               return ' - '.$sql_object->TC_Caption.' - '.$sql_object->SC_Caption;
        }

        $_URL 		= new classURL();
        $_Product   = new classProduct();
		$iProdID	= $_URL->GetValueFromURL($_URL->GetProduct(), 'product');
        $iCatID 	= $_URL->GetValueFromURL($_URL->GetProduct(), 'category');
        $result     = '';

        if(is_numeric($iCatID) and $iCatID>0)
            $result = getTopSubCategoryInfo($iCatID);

        if(is_numeric($iProdID) and $iProdID>0){
            $asProductInfo = self::getProductInfo($iProdID);
            $result .= ' - '.$asProductInfo['Product_Name'];
        }
        return $result;
    }

    public function categoryExists($iCID){
        if((trim($iCID)=='') or !is_numeric($iCID) or ($iCID<1)) return false;
        $sql_result = mysql_query('SELECT COUNT(*) FROM sub_category WHERE ID='.$iCID);
        $sql_row = mysql_fetch_row($sql_result);
        if($sql_row[0]>0) return true; else return false;
    }

    public function productExists($iPID){
        if((trim($iPID)=='') or !is_numeric($iPID) or ($iPID<1)) return false;
        $sql_result = mysql_query('SELECT COUNT(*) FROM products WHERE ID='.$iPID);
        $sql_row = mysql_fetch_row($sql_result);
        if($sql_row[0]>0) return true; else return false;
    }
}
?>
